<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{
    public function __construct(){
        //文章浏览无需认证
        $this->middleware('auth')->except('index','all','show');
    }
    /**
     * 文章列表视图
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('posts');
    }



    /**
     * 渲染文章发布表单视图
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('form');
    }

    /**
     * 新文章保存操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'title'=>'required|max:128',
            'author'=>'required',
            'content'=>'required',
        ]);

        $post = new Post();
        $post->fill($data);
        $post->status = 1;
        $post->user_id = Auth::user()->id;

        if($post->save()){
            return ['success'=>true,'message'=>'文章发布成功'];
        }
        return ['success'=>false,'message'=>'保存文章数据失败'];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        return view('posts.show',['id'=>$post->id,'pageTitle'=>$post->title]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit(Post $post)
    {
        return view('posts.edit',['pageTitle'=>'编辑文章','id'=>$post->id]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Post $post)
    {
        $data = $request->validate([
            'title'=>'required|max:128',
            'content'=>'required'
        ]);

        $post->fill($data);
        $post->status = 1;
        if($post->save()){
            return  ['success'=>true, 'message'=>'文章更新成功'];
        }
        return ['success'=>false, 'message'=>'文章更新数据失败！'];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy(Post $post)
    {
        if($post->delete()){
            return  ['success'=>true, 'message'=>'文章删除成功'];
        }
        return  ['success'=>false, 'message'=>'文章删除失败'];
    }

/**
     * 客户端通过 Axios 异步获取所有文章数据
     *获得所有文章数据，根据创建时间倒叙
     * @return void
     */
    public function all()
    {
        return Post::orderByDesc('created_at')->get();
    }

    /**
     * 获得单个文章数据
     *
     * @param Post $post
     * @return void
     */
    public function data(Post $post)
    {
        $post->author_name = $post->author->name;
        return $post;
    }
}
